import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from scipy import stats

plt.rcParams['font.sans-serif'] = ['SimHei']  # 设置中文显示
plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题

# 读取新数据
df = pd.read_csv('数据/new.csv')
print("原始数据预览：")
print(df.head())

# 创建保存图片的文件夹
import os
if not os.path.exists('图表'):
    os.makedirs('图表')

# 基本统计分析
print("数据基本统计信息：")
print(df.describe())

# 检查缺失值
print("\n缺失值统计：")
print(df.isnull().sum())

# 数据预处理 - 将分类变量转换为数值
df_numeric = df.copy()
df_numeric['Gender'] = df_numeric['Gender'].replace({'Male': 1, 'Female': 0})
df_numeric['Polyuria'] = df_numeric['Polyuria'].replace({'Yes': 1, 'No': 0})
df_numeric['Polydipsia'] = df_numeric['Polydipsia'].replace({'Yes': 1, 'No': 0})
df_numeric['sudden weight loss'] = df_numeric['sudden weight loss'].replace({'Yes': 1, 'No': 0})
df_numeric['weakness'] = df_numeric['weakness'].replace({'Yes': 1, 'No': 0})
df_numeric['Polyphagia'] = df_numeric['Polyphagia'].replace({'Yes': 1, 'No': 0})
df_numeric['Genital thrush'] = df_numeric['Genital thrush'].replace({'Yes': 1, 'No': 0})
df_numeric['visual blurring'] = df_numeric['visual blurring'].replace({'Yes': 1, 'No': 0})
df_numeric['Itching'] = df_numeric['Itching'].replace({'Yes': 1, 'No': 0})
df_numeric['Irritability'] = df_numeric['Irritability'].replace({'Yes': 1, 'No': 0})
df_numeric['delayed healing'] = df_numeric['delayed healing'].replace({'Yes': 1, 'No': 0})
df_numeric['partial paresis'] = df_numeric['partial paresis'].replace({'Yes': 1, 'No': 0})
df_numeric['muscle stiffness'] = df_numeric['muscle stiffness'].replace({'Yes': 1, 'No': 0})
df_numeric['Alopecia'] = df_numeric['Alopecia'].replace({'Yes': 1, 'No': 0})
df_numeric['Obesity'] = df_numeric['Obesity'].replace({'Yes': 1, 'No': 0})
df_numeric['class'] = df_numeric['class'].replace({'Positive': 1, 'Negative': 0})

# 1. 年龄分布与糖尿病关系
plt.figure(figsize=(10, 6))
sns.histplot(data=df, x='Age', hue='class', multiple='stack', bins=20)
plt.title('年龄分布与糖尿病关系')
plt.xlabel('年龄')
plt.ylabel('频数')
plt.savefig('图表/年龄分布与糖尿病关系.png')
# plt.show()

# 2. 性别与糖尿病关系
plt.figure(figsize=(8, 6))
gender_diabetes = pd.crosstab(df['Gender'], df['class'])
gender_diabetes.plot(kind='bar', stacked=True)
plt.title('性别与糖尿病关系')
plt.xlabel('性别')
plt.ylabel('人数')
plt.xticks(rotation=0)
plt.savefig('图表/性别与糖尿病关系.png')
# plt.show()

# 3. 相关性分析
# 计算相关性
corr_matrix = df_numeric.corr()

# 绘制热力图
plt.figure(figsize=(16, 14))
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', fmt='.2f', linewidths=0.5)
plt.title('特征相关性热力图')
plt.tight_layout()
plt.savefig('图表/相关性热力图.png')
# plt.show()

# 4. 各症状与糖尿病的关系
symptoms = ['Polyuria', 'Polydipsia', 'sudden weight loss', 'weakness', 
            'Polyphagia', 'Genital thrush', 'visual blurring', 'Itching', 
            'Irritability', 'delayed healing', 'partial paresis', 
            'muscle stiffness', 'Alopecia', 'Obesity']

plt.figure(figsize=(20, 15))
for i, symptom in enumerate(symptoms):
    plt.subplot(4, 4, i+1)
    symptom_diabetes = pd.crosstab(df[symptom], df['class'])
    symptom_diabetes.plot(kind='bar', stacked=True, ax=plt.gca())
    plt.title(f'{symptom}与糖尿病关系')
    plt.xlabel(symptom)
    plt.ylabel('人数')
    plt.xticks(rotation=0)
plt.tight_layout()
plt.savefig('图表/症状与糖尿病关系.png')
# plt.show()

# 5. 症状比例分析
plt.figure(figsize=(14, 10))
for i, symptom in enumerate(symptoms):
    plt.subplot(4, 4, i+1)
    symptom_ratio = pd.crosstab(df[symptom], df['class'], normalize='index')
    symptom_ratio.plot(kind='bar', stacked=True, ax=plt.gca())
    plt.title(f'{symptom}与糖尿病比例')
    plt.xlabel(symptom)
    plt.ylabel('比例')
    plt.xticks(rotation=0)
plt.tight_layout()
plt.savefig('图表/症状与糖尿病比例.png')
# plt.show()

# 6. 年龄段分析
# 创建年龄段
df['Age_Group'] = pd.cut(df['Age'], bins=[0, 20, 30, 40, 50, 60, 100], 
                    labels=['<20岁', '20-30岁', '30-40岁', '40-50岁', '50-60岁', '>60岁'])

# 年龄段与糖尿病关系
plt.figure(figsize=(12, 6))
age_group_diabetes = pd.crosstab(df['Age_Group'], df['class'], normalize='index')
age_group_diabetes.plot(kind='bar', stacked=True)
plt.title('不同年龄段糖尿病比例')
plt.xlabel('年龄段')
plt.ylabel('比例')
plt.xticks(rotation=0)
plt.savefig('图表/不同年龄段糖尿病比例.png')
# plt.show()

# 7. 主成分分析(PCA)可视化
# 准备数据
X = df_numeric.drop(['class'], axis=1)
y = df_numeric['class']

# 标准化
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# PCA降维
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)

# 创建PCA结果的DataFrame
pca_df = pd.DataFrame(data=X_pca, columns=['主成分1', '主成分2'])
pca_df['class'] = y

# 绘制PCA结果
plt.figure(figsize=(10, 8))
sns.scatterplot(x='主成分1', y='主成分2', hue='class', data=pca_df, palette='Set1', s=100)
plt.title('PCA降维结果')
plt.xlabel(f'主成分1 (解释方差: {pca.explained_variance_ratio_[0]:.2f})')
plt.ylabel(f'主成分2 (解释方差: {pca.explained_variance_ratio_[1]:.2f})')
plt.savefig('图表/PCA降维结果.png')
# plt.show()

# 8. 特征重要性分析
# 计算每个特征与目标变量的相关性
feature_importance = corr_matrix['class'].drop('class').abs().sort_values(ascending=False)

plt.figure(figsize=(12, 8))
feature_importance.plot(kind='bar')
plt.title('特征重要性(基于相关性)')
plt.xlabel('特征')
plt.ylabel('与糖尿病的相关性(绝对值)')
plt.xticks(rotation=45, ha='right')
plt.tight_layout()
plt.savefig('图表/特征重要性.png')
# plt.show()

# 9. 散点图矩阵 - 选择前4个重要特征
top_features = feature_importance.head(4).index.tolist()
top_features.append('class')
plt.figure(figsize=(16, 12))
sns.pairplot(df_numeric[top_features], hue='class', palette='Set1')
plt.suptitle('重要特征散点图矩阵', y=1.02)
plt.savefig('图表/重要特征散点图矩阵.png')
# plt.show()

# 10. 年龄与症状的关系
plt.figure(figsize=(20, 15))
for i, symptom in enumerate(symptoms[:12]):  # 选择前12个症状
    plt.subplot(4, 3, i+1)
    sns.boxplot(x=symptom, y='Age', hue='class', data=df)
    plt.title(f'年龄与{symptom}的关系')
plt.tight_layout()
plt.savefig('图表/年龄与症状关系.png')
# plt.show()

# 11. 多变量分析 - 性别、年龄与糖尿病
plt.figure(figsize=(12, 8))
# 创建年龄组
df['Age_Group_Simple'] = pd.cut(df['Age'], bins=[0, 30, 50, 100], 
                          labels=['年轻(<30)', '中年(30-50)', '老年(>50)'])
for i, gender in enumerate(['Male', 'Female']):
    plt.subplot(1, 2, i+1)
    gender_age_diabetes = pd.crosstab(df[df['Gender'] == gender]['Age_Group_Simple'], 
                                     df[df['Gender'] == gender]['class'])
    gender_age_diabetes.plot(kind='bar', stacked=True, ax=plt.gca())
    plt.title(f'{gender}不同年龄段糖尿病分布')
    plt.xlabel('年龄段')
    plt.ylabel('人数')
plt.tight_layout()
plt.savefig('图表/性别年龄与糖尿病关系.png')
# plt.show()

# 12. 症状组合分析
# 计算每个人的症状数量
df['Symptom_Count'] = df[symptoms].applymap(lambda x: 1 if x == 'Yes' else 0).sum(axis=1)

# 绘制症状数量与糖尿病关系
plt.figure(figsize=(10, 6))
sns.countplot(x='Symptom_Count', hue='class', data=df)
plt.title('症状数量与糖尿病关系')
plt.xlabel('症状数量')
plt.ylabel('人数')
plt.savefig('图表/症状数量与糖尿病关系.png')
# plt.show()

# 13. 症状组合热图
# 创建症状组合矩阵
symptom_combinations = df[symptoms].applymap(lambda x: 1 if x == 'Yes' else 0).T.dot(df[symptoms].applymap(lambda x: 1 if x == 'Yes' else 0))
np.fill_diagonal(symptom_combinations.values, 0)  # 对角线设为0

plt.figure(figsize=(14, 12))
sns.heatmap(symptom_combinations, annot=True, cmap='YlGnBu')
plt.title('症状组合热图')
plt.tight_layout()
plt.savefig('图表/症状组合热图.png')
# plt.show()

# 14. 综合分析报告
print("\n=== 糖尿病风险因素分析报告 ===")
print("\n1. 数据概览:")
print(f"  - 总样本数: {len(df)}")
print(f"  - 糖尿病患者数: {len(df[df['class'] == 'Positive'])}")
print(f"  - 健康人数: {len(df[df['class'] == 'Negative'])}")

print("\n2. 主要风险因素(基于相关性):")
for feature, corr in feature_importance.head(5).items():
    print(f"  - {feature}: {corr:.4f}")

print("\n3. 年龄分析:")
age_stats = df.groupby('class')['Age'].agg(['mean', 'std', 'min', 'max'])
print(age_stats)

print("\n4. 症状分析:")
for symptom in symptoms:
    symptom_positive = df[df['class'] == 'Positive'][symptom].value_counts(normalize=True)
    symptom_negative = df[df['class'] == 'Negative'][symptom].value_counts(normalize=True)
    print(f"\n{symptom}:")
    print(f"  - 患病人群中有此症状比例: {symptom_positive.get('Yes', 0):.2%}")
    print(f"  - 健康人群中有此症状比例: {symptom_negative.get('Yes', 0):.2%}")
    print(f"  - 差异: {(symptom_positive.get('Yes', 0) - symptom_negative.get('Yes', 0)):.2%}")

# 保存分析报告
with open('图表/分析报告.txt', 'w', encoding='utf-8') as f:
    f.write("=== 糖尿病风险因素分析报告 ===\n")
    f.write(f"\n1. 数据概览:\n")
    f.write(f"  - 总样本数: {len(df)}\n")
    f.write(f"  - 糖尿病患者数: {len(df[df['class'] == 'Positive'])}\n")
    f.write(f"  - 健康人数: {len(df[df['class'] == 'Negative'])}\n")
    
    f.write("\n2. 主要风险因素(基于相关性):\n")
    for feature, corr in feature_importance.head(5).items():
        f.write(f"  - {feature}: {corr:.4f}\n")
    
    f.write("\n3. 年龄分析:\n")
    f.write(str(age_stats) + "\n")
    
    f.write("\n4. 症状分析:\n")
    for symptom in symptoms:
        symptom_positive = df[df['class'] == 'Positive'][symptom].value_counts(normalize=True)
        symptom_negative = df[df['class'] == 'Negative'][symptom].value_counts(normalize=True)
        f.write(f"\n{symptom}:\n")
        f.write(f"  - 患病人群中有此症状比例: {symptom_positive.get('Yes', 0):.2%}\n")
        f.write(f"  - 健康人群中有此症状比例: {symptom_negative.get('Yes', 0):.2%}\n")
        f.write(f"  - 差异: {(symptom_positive.get('Yes', 0) - symptom_negative.get('Yes', 0)):.2%}\n")

print("\n分析完成！所有图表已保存到'图表'文件夹中。")